From a6589681351dc4a87b6fc268d47b7d608004b455 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 25 Aug 2014 05:54:47 -0700 Subject: [PATCH] Add a `harness` manifest option This option is used to disable the --test flag to rustc for a test or benchmark target in order to signal that the binary already knows how to run the testing infrastructure. The test/benchmark is still compiled and run as usual, and the exit code is expected to reflect the result of the test/benchmark. Closes #431 --- src/cargo/core/manifest.rs | 14 +++++++++++++- src/cargo/ops/cargo_rustc/mod.rs | 2 +- src/cargo/util/toml.rs | 8 ++++++-- tests/test_cargo_test.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index fe590f119..174e751aa 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -114,6 +114,7 @@ pub struct Profile { doc: bool, dest: Option, plugin: bool, + harness: bool, // whether to use the test harness (--test) } impl Profile { @@ -127,6 +128,7 @@ impl Profile { dest: None, plugin: false, doctest: false, + harness: true, } } @@ -189,6 +191,10 @@ impl Profile { self.test } + pub fn uses_test_harness(&self) -> bool { + self.harness + } + pub fn is_doctest(&self) -> bool { self.doctest } @@ -242,6 +248,11 @@ impl Profile { self.plugin = plugin; self } + + pub fn harness(mut self, harness: bool) -> Profile { + self.harness = harness; + self + } } impl hash::Hash for Profile { @@ -253,6 +264,7 @@ impl hash::Hash for Profile { debug, plugin, dest: ref dest, + harness: harness, // test flags are separated by file, not by profile hash, and // env/doc also don't matter for the actual contents of the output @@ -262,7 +274,7 @@ impl hash::Hash for Profile { test: _, doctest: _, } = *self; - (opt_level, debug, plugin, dest).hash(into) + (opt_level, debug, plugin, dest, harness).hash(into) } } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index a4644e174..6b7254267 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -310,7 +310,7 @@ fn build_base_args(mut cmd: ProcessBuilder, cmd = cmd.args(["--cfg", "ndebug"]); } - if profile.is_test() { + if profile.is_test() && profile.uses_test_harness() { cmd = cmd.arg("--test"); } diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 03445dccd..d205af3af 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -500,6 +500,7 @@ struct TomlTarget { bench: Option, doc: Option, plugin: Option, + harness: Option, } #[deriving(Decodable,Encodable,PartialEq,Clone)] @@ -519,6 +520,7 @@ impl TomlTarget { bench: None, doc: None, plugin: None, + harness: None, } } } @@ -660,12 +662,13 @@ fn normalize(libs: &[TomlLibTarget], let path = test.path.clone().unwrap_or_else(|| { TomlString(default(test)) }); + let harness = test.harness.unwrap_or(true); // make sure this metadata is different from any same-named libs. let mut metadata = metadata.clone(); metadata.mix(&format!("test-{}", test.name)); - let profile = &Profile::default_test(); + let profile = &Profile::default_test().harness(harness); dst.push(Target::test_target(test.name.as_slice(), &path.to_path(), profile, @@ -680,12 +683,13 @@ fn normalize(libs: &[TomlLibTarget], let path = bench.path.clone().unwrap_or_else(|| { TomlString(default(bench)) }); + let harness = bench.harness.unwrap_or(true); // make sure this metadata is different from any same-named libs. let mut metadata = metadata.clone(); metadata.mix(&format!("bench-{}", bench.name)); - let profile = &Profile::default_bench(); + let profile = &Profile::default_bench().harness(harness); dst.push(Target::bench_target(bench.name.as_slice(), &path.to_path(), profile, diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index cd172fe7d..f6f492389 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -874,3 +874,33 @@ test!(test_no_run { compiling = COMPILING, dir = p.url()).as_slice())); }) + +test!(test_no_harness { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [[bin]] + name = "foo" + test = false + + [[test]] + name = "bar" + path = "foo.rs" + harness = false + "#) + .file("src/main.rs", "fn main() {}") + .file("foo.rs", "fn main() {}"); + + assert_that(p.cargo_process("test"), + execs().with_status(0) + .with_stdout(format!("\ +{compiling} foo v0.0.1 ({dir}) +{running} target[..]bar-[..] +", + compiling = COMPILING, running = RUNNING, + dir = p.url()).as_slice())); +}) -- 2.30.2